﻿using Abp.Web.Mvc.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WanFang.Test;
using WanFang.Web.Controllers;

namespace WanFang.Web.Controller
{
    [AbpMvcAuthorize]
    public class TestDynamicFieldController : WanFangControllerBase
    {
        private IMaterialService _mservice;
        private IDynamicFieldService _dyservice;
        private IJianCeService _jcservice;
        public TestDynamicFieldController(IMaterialService mservice, IDynamicFieldService service, IJianCeService jcservice)
        {
            _mservice = mservice;
            _dyservice = service;
            _jcservice = jcservice;
        }
        // GET: Test
        //public ActionResult List()
        //{
        //    return View(_mservice.GetList());
        //}

        public ActionResult Add()
        {
            return View();
        }
        public ActionResult FormDesign(int id)
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> FormDesign(int id, FormCollection frm)
        {
            string[] fname = frm.GetValues("fname");
            string[] ftype = frm.GetValues("ftype");
            await _dyservice.CreateField(id, fname, ftype);
            return Json(new ResultMsg());
        }
        public ActionResult Form(int id)
        {
            ViewBag.Material = _mservice.Get(id);
            return View(_dyservice.GetList(id));
        }
        [HttpPost]
        public ActionResult Form(int id, FormCollection frm)
        {
            var entity = new JianCe { Material_Id = id, JianCeDate = DateTime.Now, Supplier = "供应商xx", JianCeResult = true };
            id = _jcservice.Add(entity);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach(string key in frm.AllKeys)
            {
                if(key.IndexOf("FField") == 0)
                {
                    dic.Add(key, frm[key]);
                }
            }
            _dyservice.AddJianCeItem(id, dic);
            return Json(new ResultMsg());
        }
    }
}